package com.agent.service.impl;

import com.agent.entity.Dict;
import com.agent.entity.User;
import com.agent.repository.DictMapper;
import com.agent.repository.UserMapper;
import com.agent.util.ApplicationContextHolder;
import com.agent.util.Constants;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service("allocationService")
public class AllocationService {

    private  Map<String,Allocation>  maps = new HashMap<>();
    @Autowired
    UserMapper userMapper;

    public  Allocation getAllocation(String address){
        if(maps.containsKey(address)){
            return maps.get(address);
        }
        Allocation allocation = new Allocation();
        maps.put(address,allocation);
        return allocation;
    }

    /**
     * @param retryIndex  重试次数
     * @param address  楼
     * @return 用户
     */
    public  User allocationUser(int retryIndex,String address){
        if("04".equals(address)){
            return null;
        }
        Allocation allocation = getAllocation(address);
        List<String> userNames = allocation.getUserIdS();
        String positon = allocation.getStep()+"";
        User in = new User();
        in.setRoleId(3);
        in.setRankId(positon);
        in.setAddressId(address);
        in.setEnabled(true);
        List<User> userList = userMapper.list(in);
        if(userList.size()==0){
            if(retryIndex < 5){
                allocation.setStep4Empty();
                return allocationUser(retryIndex+1,address);
            }
        }else{
            for(int i =0;i<userList.size();i++){
                User user = userList.get(i);
                if(userNames == null || !userNames.contains(user.getUserName())){ // 该工程师没有被选中过  或者是最后一个
                    allocation.setStep(user.getUserName());
                    return user;
                }else if(userNames.contains(user.getUserName())){
                    if(retryIndex < 5){
                        allocation.setStep4Empty();
                        return allocationUser(retryIndex+1,address);
                    }
                }
            }
        }
        return null;
    }

    /**
     * 先注释掉
     * @param retryIndex  重试次数
     * @param address  楼
     * @return 用户
     */
    /*public  User allocationUser(int retryIndex,String address){
        if("04".equals(address)){
            return null;
        }
        Allocation allocation = getAllocation(address);
        List<String> userNames = allocation.getUserIdS();
        String positon = allocation.getStep()+"";
        User in = new User();
        in.setRoleId(3);
        in.setRankId(positon);
        in.setAddressId(address);
        in.setEnabled(true);
        List<User> userList = userMapper.list(in);
        if(userList.size()==0){
            if(retryIndex < 5){
                allocation.setStep4Empty();
                return allocationUser(retryIndex+1,address);
            }
        }else{
            for(int i =0;i<userList.size();i++){
                User user = userList.get(i);
                if(userNames == null || !userNames.contains(user.getUserName())){ // 该工程师没有被选中过  或者是最后一个
                    allocation.setStep(user.getUserName());
                    return user;
                }else if(userNames.contains(user.getUserName())){
                    if(retryIndex < 5){
                        allocation.setStep4Empty();
                        return allocationUser(retryIndex+1,address);
                    }
                }
            }
        }
        return null;
    }*/


}


class Allocation{
    private int  step = 1; // 级别
    private int level_max ; // 级别最大数量
    private int current_count;// 当前是第几个
    private List<String>  userIdS = new ArrayList<>();// 已分配的人id
    private int max=5;

    public Allocation() {
        initCount();
    }
    void initCount(){
        Dict dict = new Dict();
        dict.setCode(Constants.levelCountMap.get(step));
        List<Dict> dicts = ApplicationContextHolder.getBean(DictMapper.class).searchDict(dict);
        if(!CollectionUtils.isEmpty(dicts)){
            level_max = Integer.parseInt(dicts.get(0).getDictValue());
        }
    }

    public int getStep() {
        if(current_count == level_max){
            this.step  +=1 ;
            userIdS.clear();
            current_count = 0;
            initCount();
        }
        return step;
    }
    public void setStep4Empty(){
        if(step == max){
            step = 1;
        }else {
            this.step  +=1 ;
            initCount();
        }
        userIdS.clear();
    }

    public void setStep(String userName) {
        if(step == max){
            step = 1;
            current_count = 0;
            userIdS.clear();
            initCount();
        }else{
            if(current_count +1 <= level_max){
                current_count +=1;
                userIdS.add(userName);
            }/*else if(current_count +1 == level_max){
                this.step  +=1 ;
                userIdS.clear();
                current_count = 0;
                initCount();
            }*/
        }
    }

    public List<String> getUserIdS() {
        return userIdS;
    }

    public void setUserIdS(List<String> userIdS) {
        this.userIdS = userIdS;
    }


    public int getLevel_max() {
        return level_max;
    }

    public void setLevel_max(int level_max) {
        this.level_max = level_max;
    }
}
